﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace RainEngine.SceneManagement
{
    /// <summary>
    /// 
    /// </summary>
    public sealed class Particle3DManager
    {
        /// <summary>
        /// 保存所有Particle3DSystem的集合
        /// </summary>
        private List<Particle3DSystem> particle3DSystems = new List<Particle3DSystem>();
       
        /// <summary>
        /// 创建一个新Particle3DManager对象
        /// </summary>
        internal Particle3DManager(){}

        /// <summary>
        /// 绘制集合中的所有Particle3DSystem
        /// </summary>
        /// <param name="gameTime"></param>
        public void Draw(GameTime gameTime,Matrix view, Matrix projection)
        {
            for (int i = 0; i<particle3DSystems.Count ; i++)
            {
                particle3DSystems[i].Draw(gameTime,view,projection );
            }
        }

        /// <summary>
        /// 更新集合中的所有Particle3DSystem
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            for (int i = 0; i<particle3DSystems.Count ; i++)
            {
                particle3DSystems[i].Update(gameTime);
            }
        }

        /// <summary>
        /// 在集合中添加一个Particle3DSystem
        /// </summary>
        public void AddParticle3DSystem(Particle3DSystem particle3DSystem)
        {
            //particle3DSystem
            particle3DSystems.Add(particle3DSystem);
            particle3DSystems.Sort(drawOrderComparer);
        }

        #region 用于Particle3DSystem排序的IComparer<T>

        private CompareDrawOrder<Particle3DSystem> drawOrderComparer = new CompareDrawOrder<Particle3DSystem>();

        /// <summary>
        /// 用来对particle3DSystems集合进行排序。 
        /// Comparison基于Particle3DSystem.DrawOrder。 
        /// </summary>
        private class CompareDrawOrder<T> : IComparer<T> where T : Particle3DSystem
        {
            public int Compare(T x, T y)
            {
                return x.DrawOrder  - y.DrawOrder ;
            }
        }
        #endregion
    }
}
